package com.bazinga.contact.view;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

public class SendMailAction extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public String ipaddr = null;
	public String email = "shastry.ashwin@gmail.com";
	private String dbServer = "127.0.0.1";
	private Map<String, Object> session;
	private Long jobid;
	
	@SuppressWarnings("rawtypes")
	public String send_email() throws MessagingException, UnknownHostException, SocketException
	{
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
		
		Session session = Session.getDefaultInstance(props,
			new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("applyjobs_bazinga@gmail.com","bazinga!23");
				}
			});
 
		try {
			 Enumeration e = NetworkInterface.getNetworkInterfaces();

	         while(e.hasMoreElements()) {
	            NetworkInterface ni = (NetworkInterface) e.nextElement();
	            if(ni.getName().equals("eth0"))
	            {
	            	Enumeration e2 = ni.getInetAddresses();
		            while (e2.hasMoreElements()){
		               InetAddress ip = (InetAddress) e2.nextElement();
		               ipaddr = ip.toString();
		            }
	            }
	         }
	        
			UUID uuid = UUID.randomUUID();
			String randomUUIDString = uuid.toString();
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("applyjobs_bazinga@gmail.com"));
			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(email));
			message.setRecipients(Message.RecipientType.BCC,
					InternetAddress.parse(""));
			
			message.setSubject("Application for the job");
			message.setText("Application for the job");
			Transport.send(message);
			
			System.out.println("Done");
			return randomUUIDString;
		} 
		catch (MessagingException e) 
		{
			return "";
		}	
	}
	
	public String execute() {
		this.session = ActionContext.getContext().getSession();
		if(!session.containsKey("logged-in")) {
			return "login";
		}
		
		String dbUrl = "jdbc:mysql://" + dbServer + ":3306/";
		String dbName = "bazinga";
		String driverName = "org.gjt.mm.mysql.Driver";
		String username = "root";
		String password = "";
		Connection conn = null;
		Statement stmt = null;
		
		Long userid = Long.parseLong((String)session.get("userid").toString());
		session.remove("jobid");
		String insertQuery = "insert into applications(`userid`,`jobid`) values(" + userid + "," + jobid +");";
		try
		{
			Class.forName(driverName).newInstance();
			conn = DriverManager.getConnection(dbUrl + dbName, username, password);
			stmt = conn.createStatement();
		}
		catch (Exception e) {
			System.out.println(e.toString());
		}
		try {
			stmt.execute(insertQuery);
		} 
		
		catch (SQLException e) {
			e.printStackTrace();
			return "error";
		}
		return "success";
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Long getJobid() {
		return jobid;
	}

	public void setJobid(Long jobid) {
		this.jobid = jobid;
	}
}
